libotutil: factor out utility to parse file by line
authorJonathan Lebon <jlebon@redhat.com>
Thu, 1 Feb 2018 22:06:50 +0000 (22:06 +0000)
committerAtomic Bot <atomic-devel@projectatomic.io>
Fri, 2 Feb 2018 22:36:49 +0000 (22:36 +0000)
This will be used in the checkout CLI as well.

Closes: #1442
Approved by: cgwalters

src/libotutil/ot-fs-utils.c
src/libotutil/ot-fs-utils.h
src/ostree/ot-builtin-commit.c

index affac83ad8554b2a30f7bdb5f37de4d2d8edd083..c4fcd56f11daf7d9de65f017b992070ba9ed2fd5 100644 (file)
@@ -221,3 +221,29 @@ ot_map_anonymous_tmpfile_from_content (GInputStream *instream,
     return NULL;
   return g_mapped_file_get_bytes (mfile);
 }
+
+gboolean
+ot_parse_file_by_line (const char    *path,
+                       gboolean     (*cb)(const char*, void*, GError**),
+                       void          *cbdata,
+                       GCancellable  *cancellable,
+                       GError       **error)
+{
+  g_autofree char *contents =
+    glnx_file_get_contents_utf8_at (AT_FDCWD, path, NULL, cancellable, error);
+  if (!contents)
+    return FALSE;
+
+  g_auto(GStrv) lines = g_strsplit (contents, "\n", -1);
+  for (char **iter = lines; iter && *iter; iter++)
+    {
+      /* skip empty lines at least */
+      if (**iter == '\0')
+        continue;
+
+      if (!cb (*iter, cbdata, error))
+        return FALSE;
+    }
+
+  return TRUE;
+}
index 5743c09e178cf51fb96d846a6d0808f77baf7276..74a0fed6d84c62aff57495ecbd194c32b6012182 100644 (file)
@@ -90,4 +90,11 @@ ot_map_anonymous_tmpfile_from_content (GInputStream *instream,
 GBytes *ot_fd_readall_or_mmap (int fd, goffset offset,
                                GError **error);
 
+gboolean
+ot_parse_file_by_line (const char    *path,
+                       gboolean     (*cb)(const char*, void*, GError**),
+                       void          *cbdata,
+                       GCancellable  *cancellable,
+                       GError       **error);
+
 G_END_DECLS
index c482b114cdd4e84a0877e5bbaba28435e206e4e5..370f0e5bff711e652c40246e4ae88893276817bc 100644 (file)
@@ -124,32 +124,6 @@ static GOptionEntry options[] = {
   { NULL }
 };
 
-static gboolean
-parse_file_by_line (const char    *path,
-                    gboolean     (*cb)(const char*, void*, GError**),
-                    void          *cbdata,
-                    GCancellable  *cancellable,
-                    GError       **error)
-{
-  g_autofree char *contents =
-    glnx_file_get_contents_utf8_at (AT_FDCWD, path, NULL, cancellable, error);
-  if (!contents)
-    return FALSE;
-
-  g_auto(GStrv) lines = g_strsplit (contents, "\n", -1);
-  for (char **iter = lines; iter && *iter; iter++)
-    {
-      /* skip empty lines at least */
-      if (**iter == '\0')
-        continue;
-
-      if (!cb (*iter, cbdata, error))
-        return FALSE;
-    }
-
-  return TRUE;
-}
-
 struct CommitFilterData {
   GHashTable *mode_adds;
   GHashTable *mode_overrides;
@@ -457,16 +431,16 @@ ostree_builtin_commit (int argc, char **argv, OstreeCommandInvocation *invocatio
     {
       filter_data.mode_adds = mode_adds = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
       filter_data.mode_overrides = mode_overrides = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-      if (!parse_file_by_line (opt_statoverride_file, handle_statoverride_line,
-                               &filter_data, cancellable, error))
+      if (!ot_parse_file_by_line (opt_statoverride_file, handle_statoverride_line,
+                                  &filter_data, cancellable, error))
         goto out;
     }
 
   if (opt_skiplist_file)
     {
       skip_list = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-      if (!parse_file_by_line (opt_skiplist_file, handle_skiplist_line,
-                               skip_list, cancellable, error))
+      if (!ot_parse_file_by_line (opt_skiplist_file, handle_skiplist_line,
+                                  skip_list, cancellable, error))
         goto out;
     }